import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/base_unit_stop_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/equipment_start_shutdown_ledger/equipment_start_shutdown_ledger_controller.dart';

/// 装置开停车大检修台账
class EquipmentStartShutdownLedgerPage extends StatelessWidget {
  EquipmentStartShutdownLedgerPage({super.key}) {
    Get.lazyPut(EquipmentStartShutdownLedgerController.new);
  }
  @override
  Widget build(BuildContext context) {
    final EquipmentStartShutdownLedgerController controller = Get.find<EquipmentStartShutdownLedgerController>();
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            backgroundColor: AppColors.bg_color_grey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(controller),
                      ),
                      AppGaps.hGap8,
                      _screen(controller),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<BaseUnitStopListEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.baseUnitStopList,
                    itemBuilder: (item, index) {
                      return CustomTitleChartPage(
                        title: item.deviceName ?? '',
                        titleRightWidget: getStateView(item.checkType ?? -1),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _rowData('是否停止巡检：', item.inspectionStatus == 1 ? '开启巡检' : '停止巡检', alignment: Alignment.centerRight),
                            AppGaps.vGap12,
                            _rowData('是否计划：', item.inspectionStatus == 1 ? '是' : '否', alignment: Alignment.centerRight),
                            AppGaps.vGap12,
                            //	status 状态，0停产，1在产，2检维修
                            _rowData('状态：', _status(item.status ?? -1), alignment: Alignment.centerRight),
                          ],
                        ),
                      ).click(() {
                        controller.onClickButtonPressed(item);
                      });
                    },
                  )),
                ],
              ),
            ),
            // 右侧筛选栏
            // endDrawer: CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       _selectType('状态', controller.stateNameBack ?? '', controller),
            //       _selectType('审批状态', controller.stateApproveNameBack ?? '', controller),
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget(EquipmentStartShutdownLedgerController controller) {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX),
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入装置名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen(EquipmentStartShutdownLedgerController controller) {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _rowData(String name, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 13.sp,
          ),
        ),
        Expanded(
          child: Align(
            alignment: alignment,
            child: Text(
              value,
              style: TextStyle(
                color: const Color(0xFF495061),
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        )
      ],
    );
  }

  /// 根据状态获取状态图标
  /// 审批状态 0：待发起 1：待审批 2：审批中 3：通过 4：驳回
  static Widget getStateView(int state) {
    String? stateText;
    Color? color;
    switch (state) {
      case 0:
        stateText = '待发起';
        color = const Color(0xFF0ED0D0);
        break;
      case 1:
        stateText = '待审批';
        color = const Color(0xFFFFA500);
        break;
      case 2:
        stateText = '审批中';
        color = const Color(0xFFB9B907);
        break;
      case 3:
        stateText = '通过';
        color = const Color(0xFF0FCE0F);
        break;
      case 4:
        stateText = '已驳回';
        color = const Color(0xFFBB0CBB);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText ?? '',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  ///	状态，0停产，1在产，2检维修
  String _status(int? status) {
    switch (status) {
      case 0:
        return '停产';
      case 1:
        return '在产';
      case 2:
        return '检维修';
      default:
        return '';
    }
  }

}
